package com.chenxys.algorithm.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;

/**
 * <p>
 *      二叉树结构
 * </p>
 *
 * @author Chenxys
 * @version V1.0
 * @Package com.chenxys.algorithm.common
 * @date 2024/2/22 11:01
 * @Copyright ©
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuppressWarnings("all")
public class TreeNode {
    private int val;
    private TreeNode left;
    private TreeNode right;

    /**
     * <p>
     *     打印二叉树的值
     * </p>
     */
    public static void printTreeNode(TreeNode tree) {
        if (Objects.isNull(tree)) return;
        getTreeNode(tree).forEach(s -> System.out.print(s + "\t"));
    }

    /**
     * <p>
     *     将二叉树元素从上到下 从左到右 加入至List
     * </p>
     */
    public static List<Integer> getTreeNode(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        if (!Objects.isNull(root)) queue.add(root);
        ArrayList<Integer> res = new ArrayList<>();
        while(queue.size() != 0){
            root = queue.remove();
            res.add(root.val);
            if(root.left != null){
                queue.add(root.left);
            }
            if(root.right != null){
                queue.add(root.right);
            }
        }
        return res;
    }
}
